package com.sample.ab.apis.controller;

import com.sample.ab.service.SwitchLogService;
import groovy.lang.GroovyShell;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.wisdom.annotation.WisdomController;
import org.wisdom.vo.ResponseResult;

/**
 * @author 张智研
 * @date 2022/05/30 15:59
 **/
@WisdomController
@Slf4j
@RequestMapping("/groovy")
@RequiredArgsConstructor
public class DemoController {

    private final SwitchLogService switchLogService;

    @PostMapping("/execute")
    public ResponseResult<String> execute(@RequestBody String scriptContent) {
        log.info("开始执行脚本，scriptContent={}", scriptContent);
        GroovyShell groovyShell = new GroovyShell();
        Object res = groovyShell.evaluate(scriptContent);
        log.info("222，res={}", res);
        return ResponseResult.ok(String.valueOf(res));
    }
    @PostMapping("/test")
    public ResponseResult<String> test(@RequestBody String scriptContent) {
        log.info("开始执行脚本，scriptContent={}", scriptContent);
        GroovyShell groovyShell = new GroovyShell();
        Object res = groovyShell.evaluate(scriptContent);
        log.info("结束脚本执行，res={}", res);
        return ResponseResult.ok(String.valueOf(res));
    }

    @PostMapping("/testLock")
    public ResponseResult<String> testLock() {
        try {
            switchLogService.testLock();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseResult.ok();
    }
}
